package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import object.Province;
import utility.StringUtil;
import clientMain.Login;
import clientMain.Main;

public class ProvinceDelegate {
	
	public boolean CheckInsertProId(String name){
		boolean rs = false;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select  * from Province where Province_Name =N'"+name+"'");
			  while (traVe.next()) { 
				  rs = true;
			  }
			  return rs;
			  
		} catch (Exception e) {
			
		}
		return rs;
	}
	public boolean CheckInsertProName(String name){
		boolean rs = false;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select * from Province where ProvinceName =N'"+name+"'");
			  while (traVe.next()) {
					  rs = true;
				  
			  }
			  return rs;
			  
		} catch (Exception e) {
			
		}
		return rs;
	}
	public String getProName(int  prId){
		String name = null;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select ProvinceName from Province where ProvinceId ="+prId+"");
			  while (traVe.next()) {
					  name = StringUtil.getFilterIllegal(traVe.getString("ProvinceName"));
				  
			  }
			  return name;
			  
		} catch (Exception e) {
			
		}
		return name;
	}
	public Vector<Province> getListProvince(int page, String sort, boolean flag){
		Vector<Province> list = new Vector<Province>();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_ListProvince(?,?,?,?)}");
			cst.setInt(1, Main.record);
			cst.setInt(2, page);
			cst.setString(3, sort);
			cst.setBoolean(4, flag);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				Province province = new Province();
				province.setProvinceId(rs.getInt("ProvinceId"));
				province.setProvinceName(StringUtil.getFilterIllegal(rs.getString("ProvinceName")));
				list.add(province);
			}
			Login.conn.commit();
		} catch(Exception e) {
			e.printStackTrace();
			list = null;
		}
		return list;
	}
	
	public boolean insertProvince(Province pro, int userInsert){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_InsertProvince(?, ?,?)}");
			cst.setInt(1, pro.getProvinceId());
			cst.setString(2, StringUtil.setFilterIllegal(pro.getProvinceName()));
			cst.setInt(3, userInsert);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	public boolean updateProvince(Province pro, int userUpdate){
		boolean flag = true;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateProvince(?,?,?)}");
			cst.setInt(1, pro.getProvinceId());
			cst.setString(2, StringUtil.setFilterIllegal(pro.getProvinceName()));
			cst.setInt(3, userUpdate);
			cst.executeUpdate();
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
}
